# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from lxml import etree
from fake_useragent import UserAgent
import os, time
import urllib.request

# 全局配置
save_path = 'H:\\新建文件夹\\车间工人'  # 文件路径
headers = {
    "User-Agent": UserAgent().random
}


# 下载图片
def save_imgs(img_urls):
    if not os.path.exists(save_path):  # 判断文件路径是否存在，若不在，则创建
        os.mkdir(save_path)
    for img, i in zip(img_urls, range(3000, 3000 + len(img_urls))):
        path_name = save_path + os.path.sep + str(i) + '.jpg'
        try:
            urllib.request.urlretrieve(img, path_name)  # 打开图片地址，下载图片保存在本地，
        except:
            print("error")


# 一级页面爬取
def Page_Level(myPage):  # 一级页面
    dom = etree.HTML(myPage)
    results = []
    img_urls = dom.xpath('//*[@class ="imgpage"]/ul/li/div/a/img/@src')
    for img in img_urls:
        results.append(img)
    return results


# 爬虫
def spider(myPage):
    results = Page_Level(myPage)  # 爬取
    # 选择保存方式
    save_imgs(results)


# 运行
if __name__ == "__main__":
    url = "https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%B3%B5%BC%E4%B9%A4%C8%CB%CD%BC%C6%AC&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111"
    driver = webdriver.Chrome()
    driver.get(url)
    # 就是这么简单，修改这个元素的scrollTop就可以
    time.sleep(50)
    myPage = driver.page_source  # 获取源码
    spider(myPage)
    driver.quit()
